package com.vtc.session13;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vtc.R;

public class StudentAdapter extends BaseAdapter {

	private List<Student> studentList;
	private Context context;
	private SQLAdapter sqlAdapter;
	public StudentAdapter(Context context){
		this.context = context;		
		this.sqlAdapter = new SQLAdapter(context);
		studentList = sqlAdapter.fetchALl();
	}
	@Override
	public int getCount() {
		return studentList.size();
	}

	@Override
	public Object getItem(int position) {
		return studentList.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int postion, View view, ViewGroup arg2) {
		LinearLayout layout = null;
		if(view==null){
			LayoutInflater inflat =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    layout = (LinearLayout) inflat.inflate(com.vtc.R.layout.liststudent_item, null);
		}
		else{
			layout = (LinearLayout) view;
		}
		TextView txtName = (TextView) layout.findViewById(R.id.student_txtName);
		TextView txtAddr = (TextView) layout.findViewById(R.id.student_txtAddr);
		TextView txtAge  = (TextView) layout.findViewById(R.id.student_txtAge);
		
		Student st = studentList.get(postion);
		txtName.setText(st.getName());
		txtAddr.setText(st.getAddr());
		txtAge.setText(st.getAge()+"");
		
		return layout;
	}
	public void add(Student st){
		sqlAdapter.add(st);
		studentList = sqlAdapter.fetchALl();
		notifyDataSetChanged();
	}
	public void update(Student st){
		sqlAdapter.update(st);
		studentList = sqlAdapter.fetchALl();
		notifyDataSetChanged();
	}
	public void delete(int id){
		sqlAdapter.delete(id);
		studentList = sqlAdapter.fetchALl();
		notifyDataSetChanged();
	}
}
